package com.dwy2002.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.dwy2002.common.Result;
import com.dwy2002.common.exception.EmptyIdException;
import com.dwy2002.common.utils.Validator;
import com.dwy2002.pojo.dto.users.RoleUserDTO;
import com.dwy2002.pojo.po.users.Role;
import com.dwy2002.pojo.po.users.RoleUser;
import com.dwy2002.pojo.po.users.Users;
import com.dwy2002.user.mapper.RoleUserMapper;
import com.dwy2002.user.service.IRoleUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.dwy2002.common.Result.handleResult;
import static com.dwy2002.common.utils.Utils.isEmptyOrNull;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/16
 */
@Service
@RequiredArgsConstructor
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements IRoleUserService {
    /**
     * 修改用户角色（管理员）
     *
     * @author 杜伟毅
     * @since 2025/3/16 0016 23:41
     */
    @Override
    public Result<Void> updateRoleUser(RoleUserDTO dto) {
        Validator.of(dto)
                .notEmpty(RoleUserDTO::getUserId, "用户id不允许为空")
                .notEmpty(RoleUserDTO::getNewRoleId, "新的角色不允许为空")
                .mustSatisfy(ObjectUtils.isEmpty(Db.getById(dto.getUserId(), Users.class)), "用户不存在")
                .mustSatisfy(ObjectUtils.isEmpty(Db.getById(dto.getNewRoleId(), Role.class)), "角色不存在")
                .validate();

        LambdaUpdateWrapper<RoleUser> luw = new LambdaUpdateWrapper<>();
        luw.eq(RoleUser::getUserId, dto.getUserId())
                .set(RoleUser::getRoleId, dto.getNewRoleId());
        boolean update = update(luw);
        return handleResult(update, "角色修改成功", "角色修改失败");
    }

    /**
     * 修改用户角色（系统）
     *
     * @param roleId 角色id
     * @author 杜伟毅
     * @since 2025/3/16 0016 23:41
     */
    @Override
    public void updateRoleUsers(String roleId) {
        if (isEmptyOrNull(roleId)) throw new EmptyIdException("updateRoleUsers");
        if (roleId.equals("d39bfb53b5e9352714b3f4288ab4ae5f")) throw new EmptyIdException();
        String newRoleId = "0f3b0695037abf94c7377b7a4003b99d";
        LambdaUpdateWrapper<RoleUser> luw = new LambdaUpdateWrapper<>();
        luw.eq(RoleUser::getRoleId, roleId)
                .set(RoleUser::getRoleId, newRoleId);
        update(luw);
    }

    /**
     * 根据用户id查找角色id
     *
     * @param id 用户id
     * @return 角色id
     * @author 杜伟毅
     * @since 2025/5/12 0012 21:20
     */
    @Override
    public String getRoleIdByUserId(String id) {
        if (isEmptyOrNull(id)) throw new EmptyIdException("getRoleIdByUserId");
        LambdaQueryWrapper<RoleUser> lqw = new LambdaQueryWrapper<RoleUser>().eq(RoleUser::getUserId, id);

        RoleUser roleUser = getOne(lqw);

        return Optional.ofNullable(roleUser)
                .map(RoleUser::getRoleId)
                .filter(roleId -> !StringUtils.isEmpty(roleId))
                .orElse("无角色");
    }
}
